package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.Account;
import com.example.entity.dto.AccountDetails;
import com.example.entity.vo.request.DetailsSaveVo;
import com.example.mapper.AccountDetailsMapper;
import com.example.service.AccountDetailsService;
import com.example.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @Author: shenjianZ
 * @Date: 2024/4/20 上午10:34
 * @Description:
 */

@Service
public class AccountDetailsServiceImpl extends ServiceImpl<AccountDetailsMapper, AccountDetails> implements AccountDetailsService {

    @Resource
    private AccountService accountService;

    @Override
    public AccountDetails findAccountDetailsById(Integer id) {
        return getById(id);
    }

    @Override
    @Transactional
    public boolean saveAccountDetails(Integer id, DetailsSaveVo vo) {
        Account account = accountService.findAccountByNameOrEmail(vo.getUsername());
        if (Objects.isNull(account) || Objects.equals(account.getId(), id)) {
            accountService.update()
                    .eq("id", id)
                    .set("username", vo.getUsername())
                    .update();
            saveOrUpdate(new AccountDetails(
                    id,
                    vo.getGender(),
                    vo.getPhone(),
                    vo.getQq(),
                    vo.getWx(),
                    vo.getDesc()
            ));
            return true;
        }
        return false;
    }
}
